/**
 *
 */
package ua.at.tsvetkov.samizdat.activities;

import ua.at.tsvetkov.samizdat.adapters.AdtFavoriteBooks;
import ua.at.tsvetkov.samizdat.data.Book;
import ua.at.tsvetkov.samizdat.data.FavoriteBooks;
import ua.at.tsvetkov.samizdat.data.WrongFieldCountException;
import ua.at.tsvetkov.util.Log;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class AcFavoriteBooks extends AcTemplate {

	private FavoriteBooks	favoriteBooks;
	private AdtFavoriteBooks	adtLoadedBooks;
	private String[]			books;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		favoriteBooks = new FavoriteBooks(this);
		favoriteBooks.load();
		if (favoriteBooks.data.size() == 0) {
			Toast.makeText(this, "Данные отсутствуют", Toast.LENGTH_LONG);
			finish();
		}
		books = favoriteBooks.getBookData();
		adtLoadedBooks = new AdtFavoriteBooks(this, books);
		addListView();
		listView.setAdapter(adtLoadedBooks);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Book book = null;
				try {
					book = new Book(books[position]);
				} catch (WrongFieldCountException e) {
					Log.w(e);
				}
				book.openAcBook(AcFavoriteBooks.this);
			}

		});
	}

}
